import {history, post, message, get} from '@/utils/utils'
import {appId} from '@/config'

const register = {
  namespaced: true,
  state: {
    agentInfo: {}
  },

  mutations: {
    save(state, { field, payload }) {
      state[field] = { ...state[field], ...payload }
    }
  },

  actions: {
    register: async ({ commit, dispatch }, {encryptedData, iv, phone, code, agentPromoteCode, type, wxMiniCode}) => {
      // const wxMiniCode = await dispatch('login', null, {root: true})

      // console.log(wxMiniCode)

      post({
        api: '/agt/agentinfo/register',
        data: {
          appId,
          encryptedData,
          iv,
          phone,
          code,
          agentPromoteCode,
          type,
          wxMiniCode
        },
        success: (res) => {
          message.success('注册成功')
            .then(() => {
              setTimeout(() => {
                dispatch('login/wxLogin', undefined, {root: true})
                // history.replace('/pages/login/index')
              }, 1500)
            })
        }})
    },

    getAgentInfo({ commit }, agentPromoteCode) {
      get({api: `/agt/agentinfo/scanAgentQRcodes/${agentPromoteCode}`,
        headers: {
          isToken: 'false'
        },
        success: ({data}) => {
          console.log(data)
          commit('save', {field: 'agentInfo', payload: {...data, agentPromoteCode}})
        }})
    }
  }
}

export default register
